package
{
	import dynamicStreaming.DynamicStreamingExample;
	
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.MouseEvent;
	
	import org.osmf.containers.MediaContainer;
	import org.osmf.events.PlayEvent;
	import org.osmf.layout.ScaleMode;
	import org.osmf.media.DefaultMediaFactory;
	import org.osmf.media.MediaElement;
	import org.osmf.media.MediaPlayer;
	import org.osmf.media.MediaPlayerSprite;
	import org.osmf.media.MediaResourceBase;
	import org.osmf.media.URLResource;
	
	import proxyElementsExample.ProxyElementExample;
	
	import rtmpExample.RtmpExample;
	
	public class OSMFDemo extends Sprite
	{
		public function OSMFDemo()
		{
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			
			if(0)
				addAPlayer();
			if(0)
				useMediaPlayerSprite();
			if(0)
				new DynamicStreamingExample(stage);
			if(0)
				new ProxyElementExample(stage);
			if(1)
				new RtmpExample(stage);
		}
		
		/**
		 *play 
		 */		
		private var mediaPlayerSprite:MediaPlayerSprite;
		/**
		 *开始标志 
		 */		
		private var playBtn:Sprite;
		/**
		 * 用MediaPalyerSprite直接创建player
		 */		
		private function useMediaPlayerSprite():void
		{
			mediaPlayerSprite = new MediaPlayerSprite();
			addChild(mediaPlayerSprite);
			mediaPlayerSprite.mediaPlayer.autoPlay = false;
			mediaPlayerSprite.resource = new URLResource("q7_1.flv");
			
			//mediaPlayerSprite.width = 200;
			//mediaPlayerSprite.height = 800;
			//控制缩放模式
			mediaPlayerSprite.scaleMode = ScaleMode.STRETCH;
			
			playBtn = createPlayButton();
			addChild(playBtn);
			
			playBtn.addEventListener(MouseEvent.CLICK, playHandler);
			mediaPlayerSprite.mediaPlayer.addEventListener(PlayEvent.PLAY_STATE_CHANGE, playStateChangeHandler);
		}
		
		private function playHandler(evt:MouseEvent):void
		{
			mediaPlayerSprite.mediaPlayer.play();
		}
		
		private function playStateChangeHandler(evt:PlayEvent):void
		{
			playBtn.visible = !mediaPlayerSprite.mediaPlayer.playing;
		}
		
		private static const SIZE:Number = 100;
		private static const X:Number = 320;
		private static const Y:Number = 180;
		private static const F:Number = 1.2;
		private function createPlayButton():Sprite
		{
			var result:Sprite = new Sprite();
			var g:Graphics = result.graphics;
			g.lineStyle(1, 0, 0.5);
			g.beginFill(0xA0A0A0, 0.5);
			g.moveTo(X - SIZE / F, Y - SIZE);
			g.lineTo(X + SIZE / F, Y);
			g.lineTo(X - SIZE / F, Y + SIZE);
			g.lineTo(X - SIZE / F, Y - SIZE);
			g.endFill();
			
			return result;
		}
		
		/**
		 * 用MVC的方式建造player
		 */		
		private function addAPlayer():void
		{
			//V
			var mediaContainer:MediaContainer = new MediaContainer();
			addChild(mediaContainer);
			
			//M
			var factory:DefaultMediaFactory = new DefaultMediaFactory();
			var resource:URLResource = new URLResource("q7_2.flv");
			var mediaElement:MediaElement = factory.createMediaElement(resource);
			mediaContainer.addMediaElement(mediaElement);
			
			//C
			var mediaPlayer:MediaPlayer = new MediaPlayer();
			mediaPlayer.media = mediaElement;
		}
	}
}